package com.example.win.android_banduyujia.peopleactivity.adapter;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseExpandableListAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.example.win.android_banduyujia.R;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by Win on 2015/9/9.
 */
public class ExpandAdapter extends BaseExpandableListAdapter{

    public String[] group ={"授课时间"};
    public String[][] gridViewChild = { {"今天", "周五","周六","周日","周一", "周二", "周三"}};
    private String[][] child = { {" "}};
    public String[][]  viewchilditem = { {"9:00"},{"10:00"}, {"11:00"},{"14:00"},{"15:00"},{"16:00"},{"17:00"},{"18:00"},{"19:00"},{"20:00"}};

    private LayoutInflater  mInflater;
    private Context   mContext;

    public ExpandAdapter(Context context) {
         mInflater = LayoutInflater.from(context);
         this.mContext = context;
    }

    @Override
    public int getGroupCount() {
        return group.length;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return child[groupPosition].length;
    }

    @Override
    public Object getGroup(int groupPosition) {
        return group[groupPosition];
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return child[groupPosition][childPosition];
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        if (convertView == null) {
            mViewChild = new ViewChild();
            convertView = mInflater.inflate(R.layout.view_item_door_expandable_list, null);
            mViewChild.imageView = (ImageView) convertView.findViewById(R.id.channel_imageview_orientation);
            mViewChild.textView = (TextView) convertView.findViewById(R.id.pro_time_group_name);
            convertView.setTag(mViewChild);
        } else {
            mViewChild = (ViewChild) convertView.getTag();
        }

        if (isExpanded) {
            mViewChild.imageView.setImageResource(R.mipmap.channel_expandablelistview_top_icon);
        } else {
            mViewChild.imageView.setImageResource(R.mipmap.channel_expandablelistview_bottom_icon);
        }
        mViewChild.textView.setText(getGroup(groupPosition).toString());
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        if(convertView == null){
             mViewChild = new ViewChild();
             convertView = mInflater.inflate(R.layout.view_item_text_expandable_door, null);
             mViewChild.gridView = (GridView)convertView.findViewById(R.id.door_item_text_child_gridView);
             convertView.setTag(mViewChild);
         }else{
             mViewChild = (ViewChild) convertView.getTag();
         }

        SimpleAdapter  mSimpleAdapter = new SimpleAdapter(mContext, setGirdViewData(gridViewChild[groupPosition]),
                R.layout.view_time_today_gridview_item, new String[]{"channel_gridview_item"},
                new int[]{R.id.time_gridview_item});
        mViewChild.gridView.setAdapter(mSimpleAdapter);
        setGridViewListener(mViewChild.gridView);
        mViewChild.gridView.setSelector(new ColorDrawable(Color.TRANSPARENT));
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    /**
     * 设置gridview点击事件监听
     * @param gridView
     */
    public void setGridViewListener(GridView gridView) {
          gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
              @Override
              public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                  if(view instanceof TextView){
                      // 如果想要获取到哪一行，则自定义gridview的adapter，item设置2个textview一个隐藏设置id，显示哪一行
                      TextView tv = (TextView) view;
                      Toast.makeText(mContext, "position=" + position + "||" + tv.getText(), Toast.LENGTH_SHORT).show();
                      Log.e("hefeng", "gridView listaner position=" + position + "|| text=" + tv.getText());

                      switch (view.getId()){
                          case R.id.time_gridview_item:

                              break;
                          case R.id.month_item_view:

                              break;
                          case R.id.today_gridview_item:

                              break;
                      }
                  }
              }
          });
    }

    /**
     * 设置gridview数据
     * @param data
     * @return
     */
    public ArrayList<HashMap<String, Object>> setGirdViewData(String[] data) {
        ArrayList<HashMap<String, Object>>  gritItem = new ArrayList<HashMap<String, Object>>();
        for (int i =0; i<data.length; i++){
            HashMap<String, Object>  hashMap = new HashMap<String, Object>();
            hashMap.put("channel_gridview_item", data[i]);
            gritItem.add(hashMap);
        }
        return gritItem;
    }

    ViewChild   mViewChild;
    static class ViewChild{
        ImageView imageView;
        TextView textView;
        GridView gridView;
    }
}
